package com.ben.multiple.elasticsearch.template;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchClients;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.CriteriaQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.SearchTemplateQuery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

class JavaSearchTest {
    static ElasticsearchTemplate template;
    static ElasticsearchClient client;

    static {
        client = ElasticsearchClients.createImperative(ClientConfiguration.create("192.168.229.141:9200"));
        template = new ElasticsearchTemplate(client);
    }

    public static void main(String[] args) {
//        searchOne();
//        search();
//        search2();
        search3();

        client.shutdown();
        System.exit(5);
    }

    static void searchOne() {
        Criteria criteria = Criteria.where("address").matches("湖北省");
        CriteriaQuery query = new CriteriaQueryBuilder(criteria).build();
        SearchHit<HashMap> hit = template.searchOne(query, HashMap.class, IndexCoordinates.of("es_db5"));
        HashMap content = hit.getContent();
        System.out.println(content);
    }

    static void search() {
        Criteria criteria = Criteria.where("address").matches("湖北省");
        CriteriaQuery query = new CriteriaQueryBuilder(criteria).build();
        SearchHits<HashMap> hits = template.search(query, HashMap.class, IndexCoordinates.of("es_db5"));
        List<SearchHit<HashMap>> list = hits.getSearchHits();
        for (SearchHit<HashMap> hit : list) {
            System.out.println(hit.getContent());
        }
    }

    static void search2() {
        Criteria criteria = Criteria.where("address").matches("湖北省");
        criteria.and(Criteria.where("age").greaterThanEqual(30));
        CriteriaQuery query = new CriteriaQueryBuilder(criteria).build();
        SearchHits<HashMap> hits = template.search(query, HashMap.class, IndexCoordinates.of("es_db5"));
        List<SearchHit<HashMap>> list = hits.getSearchHits();
        for (SearchHit<HashMap> hit : list) {
            System.out.println(hit.getContent());
        }
    }

    static void search3() {
        Criteria criteria = Criteria.where("address").matches("湖北省");
        criteria.and(Criteria.where("name").is("张三"));
        CriteriaQuery query = new CriteriaQueryBuilder(criteria).withPageable(Pageable.ofSize(1)).build();
        SearchHits<HashMap> hits = template.search(query, HashMap.class, IndexCoordinates.of("es_db5"));
        List<SearchHit<HashMap>> list = hits.getSearchHits();
        for (SearchHit<HashMap> hit : list) {
            System.out.println(hit.getContent());
        }
    }

}
